package com.ecmcloud.flow.resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ecmcloud.common.config.NotWriteLogAnno;
import com.ecmcloud.common.dto.PageResultDto;
import com.ecmcloud.common.dto.ProcessInstanceParamDto;
import com.ecmcloud.common.dto.Result;
import com.ecmcloud.common.dto.TaskDto;
import com.ecmcloud.common.dto.TaskParamDto;
import com.ecmcloud.common.dto.TaskQueryParamDto;
import com.ecmcloud.common.dto.flow.Node;
import com.ecmcloud.flow.service.WorkflowService;

/**
 * 工作流控制器
 * 负责流程创建编辑发起等功能
 */
@RestController
@RequestMapping("flow")
public class FlowResource {

    @Autowired
    private WorkflowService workflowService;

    /**
     * 根据用户id创建流程
     * @param nodeDto
     * @param userId
     * @return
     */
    @PostMapping("create")
    public Result<String> create(@RequestBody Node nodeDto, String userId) {
        String flowId = workflowService.create(nodeDto, userId);
        return Result.success(flowId);
    }

    /**
     * 发起流程
     * @param key
     * @return
     */
    @PostMapping("start")
    public Result<String> start(@RequestBody ProcessInstanceParamDto processInstanceParamDto) {
        String processInstanceId = workflowService.start(processInstanceParamDto);
        return Result.success(processInstanceId);
    }

    /**
     * 根据流程id显示流程图
     * @param procInsId
     * @return
     */
    @NotWriteLogAnno(all = false,printResultLog = false)
    @GetMapping("showImg")
    public Result<String> showImg(String procInsId) {
        String content = workflowService.showImg(procInsId);
        return Result.success(content);
    }

    /**
     * 审批流程任务节点
     * @param taskId
     * @param approved
     */
    @PostMapping("approve")
    public void approve(String taskId, boolean approved) {
    	workflowService.approve(taskId, approved);
    }

    /**
     * 终止流程
     * @param taskParamDto
     * @return
     */
    @PostMapping("stopProcessInstance")
    public Result<String> stopProcessInstance(@RequestBody TaskParamDto taskParamDto) {
    	workflowService.stopProcessInstance(taskParamDto);
        return Result.success();
    }
    
    /**
     * 查询用户已办任务
     * @param taskQueryParamDto
     * @return
     */
    @PostMapping("queryCompletedTask")
    public Result<PageResultDto<TaskDto>> queryCompletedTask(@RequestBody TaskQueryParamDto taskQueryParamDto) {
        PageResultDto<TaskDto> pageResultDto = workflowService.queryCompletedTask(taskQueryParamDto);
        return Result.success(pageResultDto);
    }
    
    /**
     * 查询用户待办任务
     * @param taskQueryParamDto
     * @return
     */
    @PostMapping("queryAssignTask")
    public Result<PageResultDto<TaskDto>> queryAssignTask(@RequestBody TaskQueryParamDto taskQueryParamDto) {
        PageResultDto<TaskDto> pageResultDto = workflowService.queryAssignTask(taskQueryParamDto);
        return Result.success(pageResultDto);
    }

}
